//
//  RegexHelper.swift
//  Maotai
//
//  Created by BO ZHOU on 2020/12/8.
//

import UIKit

public struct RegexHelper {
    
    let regex: NSRegularExpression
    
    init(_ pattern: String, options: NSRegularExpression.Options = .caseInsensitive) throws {
        try regex = NSRegularExpression(pattern: pattern,
                                        options: options)
    }
    
    /**
     * 判断是否包含某个字符串
     */
    public func matchIfContain(_ input: String) -> Bool {
        
        let matches = regex.matches(in: input,
                                    options: [],
                                    range: NSMakeRange(0, input.utf16.count))
        return matches.count > 0
    }
    
    
    /**
     * 判断是否包含某个字符串 (类方法)
     */
    public static func matchIfContain(regularString: String, contentString: String, options: NSRegularExpression.Options = .caseInsensitive) -> Bool {
        
        guard let regex = regexWith(regularString: regularString, options: options) else {return false}
        
        return regex.firstMatch(in: contentString, options: [], range: NSRange(location: 0, length: contentString.utf16.count)) != nil
    }
    
    /**
     * 获取所有匹配结果
     * - parameter regularString: 正则表达式
     * - parameter contentString: 需要被匹配的文本
     * - parameter options: 匹配设置, 默认设置为忽略大小写
     */
    public static func match(regularString: String, contentString: String, options: NSRegularExpression.Options = .caseInsensitive) -> [NSTextCheckingResult]? {
        
        guard let regex = regexWith(regularString: regularString, options: options) else {return nil}

        
        let matches = regex.matches(in: contentString,
                                    options: [],
                                    range: NSMakeRange(0, contentString.utf16.count))
        return matches
    }
    
    
    /**
     * 获取第一个匹配结果
     */
    public static func firstMatch(regularString: String, contentString: String, options: NSRegularExpression.Options = .caseInsensitive) -> NSTextCheckingResult? {
        guard let regex = regexWith(regularString: regularString, options: options) else {return nil}
        
        return regex.firstMatch(in: contentString, options: [], range: NSRange(location: 0, length: contentString.utf16.count))
    }
    
    
    /**
     * 正则替换
     * - parameter regularString: 正则表达式
     * - parameter contentString: 需要被匹配的文本
     * - parameter replacementString: 需要替换的新内容
     * - parameter options: 匹配设置, 默认设置为忽略大小写
     */
    public static func replacingMatches(regularString: String, contentString: String, replaceString: String, options: NSRegularExpression.Options = .caseInsensitive) -> String {
        guard let regex = regexWith(regularString: regularString, options: options) else {return contentString}

        return regex.stringByReplacingMatches(in: contentString, options: [], range: NSRange(location: 0, length: contentString.utf16.count), withTemplate: replaceString)
    }
    
    
    
    /**
     * 私有方法: 创建RegularExpression
     */
    private static func regexWith(regularString: String, options: NSRegularExpression.Options) -> NSRegularExpression? {
        return try? NSRegularExpression(pattern: regularString, options: options)
    }
}

public extension String {
    
    /**
     * 通过正则替换字符串内容, 默认忽略大小写, 如果不忽略大小写, 可以直接调用string原生的repacingOccurrences方法
     */
    func regexReplacingOccurences(of regularString: String, with replaceString: String, options: NSRegularExpression.Options = .caseInsensitive)  -> String{
        return RegexHelper.replacingMatches(regularString: regularString, contentString: self, replaceString: replaceString, options: options)
    }
}
